<?php

/*
 * Niken Larasati
 * larasati.n@students.amikom.ac.id
 */

class penduduk extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('tanggal');
        $this->load->library('umur');
        $this->load->library('form_validation');
    }

    function form_penduduk() {
        $this->load->model('umum_m');
        $data['jenis_kelamin'] = $this->umum_m->ambil_data('jenis_kelamin');
        $data['pendidikan'] = $this->umum_m->ambil_data('pendidikan');
        $data['gol_darah'] = $this->umum_m->ambil_data('gol_darah');
        $data['agama'] = $this->umum_m->ambil_data('agama');
        $data['pekerjaan'] = $this->umum_m->ambil_data('pekerjaan');
        $data['kewarganegaraan'] = $this->umum_m->ambil_data('kewarganegaraan');
        $data['status_kawin'] = $this->umum_m->ambil_data('status_kawin');
        $data['status_keluarga'] = $this->umum_m->ambil_data('status_keluarga');

        $this->template->load('halaman_v', 'penduduk/tambah_penduduk_v', $data);
    }

    function tambah() { //diurutin form
        if ($this->input->post('tambah') != NULL) {
            $data['nik'] = $this->input->post('nik');
            $data['nama'] = $this->input->post('nama');
            $data['tempat_lahir'] = $this->input->post('tempat_lahir');
            $data['tgl_lahir'] = $this->input->post('tgl_lahir');
            $data['idjenis_kelamin'] = $this->input->post('idjenis_kelamin');
            $data['idpendidikan'] = $this->input->post('idpendidikan');
            $data['idgol_darah'] = $this->input->post('idgol_darah');
            $data['idagama'] = $this->input->post('idagama');
            $data['idpekerjaan'] = $this->input->post('idpekerjaan');
            $data['idkewarganegaraan'] = $this->input->post('idkewarganegaraan');
            $data['keturunan'] = $this->input->post('keturunan');
            $data['idstatus_kawin'] = $this->input->post('idstatus_kawin');
            $data['idstatus_keluarga'] = $this->input->post('idstatus_keluarga');
            $data['status'] = $this->input->post('status');
            $data['telp'] = $this->input->post('telp');
            $data['nama_ayah'] = $this->input->post('nama_ayah');
            $data['nama_ibu'] = $this->input->post('nama_ibu');
            $data['no_kk'] = $this->input->post('no_kk');
            $data['no_suratnikah'] = $this->input->post('no_suratnikah');
            $data['tgl_suratnikah'] = $this->input->post('tgl_suratnikah');
            $data['no_suratcerai'] = $this->input->post('no_suratcerai');
            $data['tgl_suratcerai'] = $this->input->post('tgl_suratcerai');
            $data['no_passpor'] = $this->input->post('no_passpor');
            $data['no_kitas'] = $this->input->post('no_kitas');

            $this->load->model('umum_m');
            $this->umum_m->tambah('penduduk', $data);
            redirect('penduduk/tampil');
        }
    }

    function ubah($id = NULL) {
        $this->load->model('umum_m');
        $data['jenis_kelamin'] = $this->umum_m->ambil_data('jenis_kelamin');
        $data['pendidikan'] = $this->umum_m->ambil_data('pendidikan');
        $data['gol_darah'] = $this->umum_m->ambil_data('gol_darah');
        $data['agama'] = $this->umum_m->ambil_data('agama');
        $data['pekerjaan'] = $this->umum_m->ambil_data('pekerjaan');
        $data['kewarganegaraan'] = $this->umum_m->ambil_data('kewarganegaraan');
        $data['status_kawin'] = $this->umum_m->ambil_data('status_kawin');
        $data['status_keluarga'] = $this->umum_m->ambil_data('status_keluarga');
        if ($id != NULL) {
            $this->load->model('umum_m');
            $data['data_penduduk'] = $this->umum_m->ambil_data('penduduk', 'nik = ' . $id)->row();
            $this->template->load('halaman_v', 'penduduk/ubah_penduduk_v', $data);
        } else {
            redirect('penduduk');
        }
    }

    function simpan_ubah() {
        if ($this->input->post() != NULL) {
            $penduduk['nik'] = $this->input->post('nik');
            $penduduk['nama'] = $this->input->post('nama');
            $penduduk['tempat_lahir'] = $this->input->post('tempat_lahir');
            $penduduk['tgl_lahir'] = $this->input->post('tgl_lahir');
            $penduduk['idjenis_kelamin'] = $this->input->post('idjenis_kelamin');
            $penduduk['idpendidikan'] = $this->input->post('idpendidikan');
            $penduduk['idgol_darah'] = $this->input->post('idgol_darah');
            $penduduk['idagama'] = $this->input->post('idagama');
            $penduduk['idpekerjaan'] = $this->input->post('idpekerjaan');
            $penduduk['idkewarganegaraan'] = $this->input->post('idkewarganegaraan');
            $penduduk['keturunan'] = $this->input->post('keturunan');
            $penduduk['idstatus_kawin'] = $this->input->post('idstatus_kawin');
            $penduduk['idstatus_keluarga'] = $this->input->post('idstatus_keluarga');
            $penduduk['status'] = $this->input->post('status');
            $penduduk['telp'] = $this->input->post('telp');
            $penduduk['nama_ayah'] = $this->input->post('nama_ayah');
            $penduduk['nama_ibu'] = $this->input->post('nama_ibu');
            $penduduk['no_kk'] = $this->input->post('no_kk');
            $penduduk['no_suratnikah'] = $this->input->post('no_suratnikah');
            $penduduk['tgl_suratnikah'] = $this->input->post('tgl_suratnikah');
            $penduduk['no_suratcerai'] = $this->input->post('no_suratcerai');
            $penduduk['tgl_suratcerai'] = $this->input->post('tgl_suratcerai');
            $penduduk['no_passpor'] = $this->input->post('no_passpor');
            $penduduk['no_kitas'] = $this->input->post('no_kitas');

            $this->load->model('umum_m');
            if ($this->umum_m->ubah('penduduk', $penduduk, 'nik', $penduduk['nik'])) {
                redirect('penduduk/tampil'); //kalau tidak ada data yg diubah?
            }
        }
    }

    function tampil() {
        $this->load->model('penduduk_m');
        $data['penduduk'] = $this->penduduk_m->ambil_data();
        $this->template->load('halaman_v', 'penduduk/tampil_penduduk_v', $data);
    }

    function hapus($id = NULL) {
        if ($id != NULL) {
            $this->load->model('umum_m');
            $status = $this->umum_m->cek_relasi_tabel('kelahiran', 'nik_ibu', $id);
            $status = $this->umum_m->cek_relasi_tabel('kelahiran', 'nik_ayah', $id);
            $status = $this->umum_m->cek_relasi_tabel('kelahiran', 'nik_pelapor', $id);
            $status = $this->umum_m->cek_relasi_tabel('kelahiran', 'nik_saksi_satu', $id);
            $status = $this->umum_m->cek_relasi_tabel('kelahiran', 'nik_saksi_dua', $id);
            $status = $this->umum_m->cek_relasi_tabel('kematian', 'nik', $id);
            $status = $this->umum_m->cek_relasi_tabel('pindah_pergi', 'nik', $id);
            if ($status == 0) {
                $this->umum_m->hapus_data_tabel('penduduk', array('nik' => $id));
                $this->tampil();
            }
        }
    }

    function cari_tahun() {
        $this->load->model('penduduk_m');
        $tahun['penduduk'] = $this->penduduk_m->detail_cari();
        $this->template->load('halaman_v', 'penduduk/cari_tahunpenduduk_v', $tahun);
    }

    function cari_bulan() {
        $this->load->model('penduduk_m');
        $bulan['penduduk'] = $this->penduduk_m->detail_cari();
        $this->template->load('halaman_v', 'penduduk/cari_bulanpenduduk_v', $bulan);
    }

    function cari_jk() {
        $this->load->model('umum_m');
        $data['jenis_kelamin'] = $this->umum_m->ambil_data('jenis_kelamin');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.idjenis_kelamin' => $this->input->post('idjenis_kelamin')));
            $data['nama_jeniskelamin'] = $this->umum_m->native_query('select jenis_kelamin from jenis_kelamin where idjenis_kelamin=' . $this->input->post('idjenis_kelamin'))->row();
            $this->template->load('halaman_v', 'penduduk/cari_jkpenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_jkpenduduk_v', $data);
        }
    }

    function cari_pendidikan() {
        $this->load->model('umum_m');
        $data['pendidikan'] = $this->umum_m->ambil_data('pendidikan');
        if ($this->input->post() != null) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.idpendidikan' => $this->input->post('idpendidikan'))); //kolom><dropdown
            $data['nama_pendidikan'] = $this->umum_m->native_query('select pendidikan from pendidikan where idpendidikan=' . $this->input->post('idpendidikan'))->row();
            $this->template->load('halaman_v', 'penduduk/cari_pendidikanpenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_pendidikanpenduduk_v', $data);
        }
    }

    function cari_goldarah() {
        $this->load->model('umum_m');
        $data['gol_darah'] = $this->umum_m->ambil_data('gol_darah');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.idgol_darah' => $this->input->post('idgol_darah')));
            $data['nama_goldarah'] = $this->umum_m->native_query('select gol_darah from gol_darah where idgol_darah=' . $this->input->post('idgol_darah'))->row();
            $this->template->load('halaman_v', 'penduduk/cari_goldarahpenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_goldarahpenduduk_v', $data);
        }
    }

    function cari_agama() {
        $this->load->model('umum_m');
        $data['agama'] = $this->umum_m->ambil_data('agama');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.idagama' => $this->input->post('idagama')));
            $data['nama_agama'] = $this->umum_m->native_query('select agama from agama where idagama=' . $this->input->post('idagama'))->row();
            $this->template->load('halaman_v', 'penduduk/cari_agamapenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_agamapenduduk_v', $data);
        }
    }

    function cari_pekerjaan() {
        $this->load->model('umum_m');
        $data['pekerjaan'] = $this->umum_m->ambil_data('pekerjaan');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.idpekerjaan' => $this->input->post('idpekerjaan')));
            $data['nama_pekerjaan'] = $this->umum_m->native_query('select pekerjaan from pekerjaan where idpekerjaan=' . $this->input->post('idpekerjaan'))->row();
            $this->template->load('halaman_v', 'penduduk/cari_pekerjaanpenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_pekerjaanpenduduk_v', $data);
        }
    }

    function cari_statuskawin() {
        $this->load->model('umum_m');
        $data['status_kawin'] = $this->umum_m->ambil_data('status_kawin');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.idstatus_kawin' => $this->input->post('idstatus_kawin')));
            $data['nama_statuskawin'] = $this->umum_m->native_query('select status_kawin from status_kawin where idstatus_kawin=' . $this->input->post('idstatus_kawin'))->row();
            $this->template->load('halaman_v', 'penduduk/cari_statuskawinpenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_statuskawinpenduduk_v', $data);
        }
    }

    function cari_statuskeluarga() {
        $this->load->model('umum_m');
        $data['status_keluarga'] = $this->umum_m->ambil_data('status_keluarga');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.idstatus_keluarga' => $this->input->post('idstatus_keluarga')));
            $data['nama_statuskeluarga'] = $this->umum_m->native_query('select status_keluarga from status_keluarga where idstatus_keluarga=' . $this->input->post('idstatus_keluarga'))->row();
            $this->template->load('halaman_v', 'penduduk/cari_statuskeluargapenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_statuskeluargapenduduk_v', $data);

        }
    }

//        function cari_kategoriumur() { //??
////        $this->load->model('umum_m');
////        $data[''] = $this->umum_m->ambil_data('');
//            $this->load->model('penduduk_m');
//            $umur['penduduk'] = $this->penduduk_m->detail_cari();
//            $this->template->load('halaman_v', 'penduduk/cari_kategoriumurpenduduk_v', $umur);
//        }

    function cari_nik() {
        $this->load->model('umum_m');
        $data['penduduk'] = $this->umum_m->ambil_data('penduduk');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.nik' =>  $this->input->post('nik')));
            //$data['penduduk'] = $this->penduduk_m->detail_cari('penduduk.nik', $this->input->post('nik'));
            $data['nik_penduduk'] = $this->umum_m->native_query('select nik from penduduk where nik=' . $this->input->post('nik'))->row();
            //$data['nik_penduduk'] = $this->input->post('nik');
            $this->template->load('halaman_v', 'penduduk/cari_nikpenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cari_nikpenduduk_v', $data);
        }
    }
    
        
    function cetak_ktp() {
        $this->load->model('umum_m');
        $data['penduduk'] = $this->umum_m->ambil_data('penduduk');
        if ($this->input->post() != NULL) {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.nik' =>  $this->input->post('nik')));
            $data['nik_penduduk'] = $this->umum_m->native_query('select nik from penduduk where nik=' . $this->input->post('nik'))->row();
            $this->template->load('halaman_v', 'penduduk/cetak_nikpenduduk_v', $data);
        } else {
            $this->load->model('penduduk_m');
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'penduduk/cetak_nikpenduduk_v', $data);
        }
    }

}
?>
